package com.zk.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zk.entity.OrderDetail;
import com.zk.entity.Orders;
import com.zk.mapper.OrderDetailMapper;
import com.zk.mapper.OrdersMapper;
import com.zk.service.IOrdersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zk.utils.SnowFlake;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ljw
 * @since 2020-11-29
 */
@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements IOrdersService {

    @Resource
    private OrdersMapper ordersMapper;
    @Resource
    private OrderDetailMapper orderDetailMapper;

    /**
     * 添加订单
     * isDel设置为1，orderDetail中orderId设置为order中的orderId
     * 1.保存order信息
     * 2.保存orderDetail信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addOrders(Orders order) {
        order.setOrderId(SnowFlake.nextId());
        order.setIsDel(0);
        if(ordersMapper.insert(order)!=0) {
            for (OrderDetail orderDetail : order.getDetail()) {
                orderDetail.setOrder_id(order.getOrderId());
                orderDetailMapper.insert(orderDetail);
            }
        }
        return true;
    }

    /**
     * 删除订单
     * @param orderId
     * @return 是否删除成功
     */
    @Override
    public boolean delOrders(Long orderId){
        UpdateWrapper<Orders> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("order_id",orderId);
        updateWrapper.set("is_del",1);
        int affectRows = ordersMapper.update(new Orders(),updateWrapper);
        return affectRows > 0;
    }

    /**
     * 修改订单
     */
    @Override
    @Transactional
    public boolean updateOrders(Orders orders){
        int affectRows = ordersMapper.updateById(orders);
        if(affectRows>0){
//            QueryWrapper<OrderDetail> orderDetailQueryWrapper= new QueryWrapper<>();
//            orderDetailQueryWrapper.eq("order_id",orders.getOrderId());
//            for(OrderDetail orderDetail:orders.getDetail()) {
//                orderDetailMapper.update(orderDetail, orderDetailQueryWrapper);
//            }
            return true;
        }else{
            return false;
        }

    }

    /**
     * 查找所有订单
     */
    @Override
    public IPage<Orders> queryAllOrders (Integer current,Integer size){
        IPage<Orders> page=new Page<>(current,size);
        //获取订单信息
        IPage<Orders> orders = ordersMapper.selectPage(page,null);
        //获取订单内容
        for(Orders order:orders.getRecords()){
            QueryWrapper<OrderDetail> orderDetailQueryWrapper= new QueryWrapper<>();
            orderDetailQueryWrapper.eq("order_id",order.getOrderId());
            order.setDetail(orderDetailMapper.selectList(orderDetailQueryWrapper));
        }
        return orders;
    }

    /**
     *根据订单ID查找订单
     */
    @Override
    public Orders queryOrdersById(Long orderId) {
        //获取订单信息
        QueryWrapper<Orders> ordersQueryWrapper= new QueryWrapper<>();
        ordersQueryWrapper.eq("order_id",orderId);
        Orders orders = ordersMapper.selectOne(ordersQueryWrapper);
        //获取订单内容
        QueryWrapper<OrderDetail> orderDetailQueryWrapper= new QueryWrapper<>();
        orderDetailQueryWrapper.eq("order_id",orderId);
        orders.setDetail(orderDetailMapper.selectList(orderDetailQueryWrapper));
        return orders;
    }

    /**
     *根据userId查找订单
     */
    @Override
    public IPage<Orders> queryOrdersByUserName(Long userId,Integer current,Integer size) {
        //获取订单信息
        QueryWrapper<Orders> ordersQueryWrapper= new QueryWrapper<>();
        ordersQueryWrapper.eq("user_id",userId);
        IPage<Orders> page=new Page<>(current,size);
        IPage<Orders> ordersIPage = ordersMapper.selectPage(page, ordersQueryWrapper);


        //获取订单内容
        for(Orders order:ordersIPage.getRecords()) {
            QueryWrapper<OrderDetail> orderDetailQueryWrapper = new QueryWrapper<>();
            orderDetailQueryWrapper.eq("order_id", order.getOrderId());
            order.setDetail(orderDetailMapper.selectList(orderDetailQueryWrapper));
        }
        return ordersIPage;
    }


}
